/*
 * File: PythagoreanTheorem.java
 * Name: Diego A. Falciola
 * Section Leader: Sam King
 * -----------------------------
 * This file is the starter file for the PythagoreanTheorem problem.
 */

import acm.program.*;

public class PythagoreanTheorem extends ConsoleProgram {

	// "compute again?" answer is compared to this value 
	private static String COMPUTE_AGAIN = "y";

	// values necessary to compute Pythagorean Theorem
	private static double a, b;
	
	/**
	 *  Read values from user and compute Pythagoream Theorem
	 */
	public void run() {
		readValues();
		
		computePythagoreanTheorem();
	
		// an extra feature in the task
		// prompt the user to reenter values
		promptComputeAgainUntilExit();
	}
	
	/**
	 * Prompts the user with a message 
	 * in order to compute theorem with new values
	 */
	private void promptComputeAgainUntilExit(){
		String computeAgain = readComputeAgain();
		
		// while the user says "y", continue reading values 
		// and computing
		while (computeAgain.equals(COMPUTE_AGAIN)){
			readValues();
			computePythagoreanTheorem();
			
			println("Compute again? (y/n):");
			computeAgain = readLine();
		}
		
		println("\n -- User exited -- End. ");
	}
	
	/**
	 * Reads values a and b from user
	 */
	private void readValues(){
		println("Enter the values to compute the Pythagorean Theorem:");
		
		a = readDouble("a: ");
		
		b = readDouble("b: ");
	}
	
	/** 
	 * Reads "y" or "n" from user
	 * @return "y" or "n"
	 */
	private String readComputeAgain(){
		println("Compute again? (y/n):");
		String computeAgain = readLine();
		
		while (!computeAgain.equals("y") && !computeAgain.equals("n")){
			println("Please enter 'y' or 'n'. Compute again? (y/n):");
			computeAgain = readLine();
		}
		
		return computeAgain;
	}
	
	/**
	 * Computes Pythagorean Theorem 
	 */
	private void computePythagoreanTheorem(){
		double c = Math.sqrt(Math.pow(a, 2)+Math.pow(b,2));
		
		println("c: " + c + "\n");
		
	}
}
